// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   SourceFile

package com.sgame.android.network;

import java.io.*;
import java.net.URLEncoder;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.*;
import org.apache.http.entity.BasicHttpEntity;

public class HttpRequest
    implements Serializable
{

    public HttpRequest()
    {
        queryParams = new ArrayList<PairValue>();
        requestHeaders = new ConcurrentHashMap<String, String>();
        requestPath = "";
    }

    public void setBody(HttpEntity body)
    {
        mHttpEntity = body;
    }

    public HttpEntity getBody()
    {
        return mHttpEntity;
    }

    public void setBody(StringNameValueMap body) throws UnsupportedEncodingException
    {
        mHttpEntity = (HttpEntity) body.getValues();
        return;
    }

    public void setPath(String path)
    {
        requestPath = path;
    }

    public String getPath()
    {
        return requestPath;
    }

    public void addQueryParam(String key, String value)
    {
        queryParams.add(new PairValue(key, value));
    }

    public ArrayList<PairValue> getQueryParams()
    {
        return queryParams;
    }

    public void clearQueryParams()
    {
        queryParams.clear();
    }

    public void setHeader(String key, String value)
    {
        requestHeaders.put(key, value);
    }

    public ConcurrentHashMap<String, String> getHeaders()
    {
        return requestHeaders;
    }

    public void removeHeader(String key)
    {
        requestHeaders.remove(key);
    }

    public void clearHeaders()
    {
        requestHeaders.clear();
    }

    public void setMethod(String method)
    {
        requestMethod = method;
    }

    public String getMethod()
    {
        if(requestMethod == null)
            return "GET";
        else
            return requestMethod;
    }

    public HttpUriRequest getUriRequest(String baseURL)
    {
    	StringBuffer stringbuilder;
    	HttpUriRequest httpRequest;
        stringbuilder = new StringBuffer(baseURL);
        stringbuilder.append(requestPath);
        if(queryParams != null && queryParams.size() != 0)
        {
        	stringbuilder.append("?");
        	try
        	{
        		stringbuilder.append(getQueryParamsString());
        	}
        	catch(UnsupportedEncodingException ex)
        	{
        	}
        }
        if(requestMethod == "POST")
        	httpRequest = MakeHttpRequest(new HttpPost(stringbuilder.toString()), mHttpEntity);
        else
        if(requestMethod == "PUT")
        	httpRequest = MakeHttpRequest(new HttpPut(stringbuilder.toString()), mHttpEntity);
        else
        if(requestMethod == "DELETE")
        	httpRequest = new HttpDelete(stringbuilder.toString());
        else
        	httpRequest = new HttpGet(stringbuilder.toString());
        if(requestHeaders.size() > 0)
        {
            java.util.Map.Entry<String,String> entry;
            for(Iterator<Entry<String, String>> iterator = requestHeaders.entrySet().iterator(); iterator.hasNext(); httpRequest.setHeader((String)entry.getKey(), (String)entry.getValue()))
                entry = (java.util.Map.Entry<String,String>)iterator.next();

        }
        return httpRequest;
    }

    private String getQueryParamsString()
        throws UnsupportedEncodingException
    {
        StringBuilder stringbuilder = new StringBuilder();
        for(Iterator<PairValue> iterator = queryParams.iterator(); iterator.hasNext(); stringbuilder.append("&"))
        {
        	PairValue g1 = iterator.next();
            stringbuilder.append(URLEncoder.encode((String)g1.name, "UTF-8"));
            stringbuilder.append("=");
            stringbuilder.append(URLEncoder.encode((String)g1.value, "UTF-8"));
        }

        stringbuilder.deleteCharAt(stringbuilder.length() - 1);
        return stringbuilder.toString();
    }

    private static HttpEntityEnclosingRequestBase MakeHttpRequest(HttpEntityEnclosingRequestBase httpentityenclosingrequestbase, HttpEntity httpentity)
    {
        if(httpentity != null)
            httpentityenclosingrequestbase.setEntity(httpentity);
        return httpentityenclosingrequestbase;
    }

    private void writeObject(ObjectOutputStream out)
        throws IOException
    {
        out.defaultWriteObject();
        if(mHttpEntity != null)
        {
            ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
            mHttpEntity.writeTo(bytearrayoutputstream);
            out.writeObject(bytearrayoutputstream.toByteArray());
            bytearrayoutputstream.close();
            out.writeUTF(mHttpEntity.getContentType().getValue());
            return;
        } else
        {
            out.writeObject(null);
            return;
        }
    }

    private void readObject(ObjectInputStream in)
        throws IOException, ClassNotFoundException
    {
        in.defaultReadObject();
        byte abyte0[];
        if((abyte0 = (byte[])in.readObject()) != null)
        {
        	mHttpEntity = new BasicHttpEntity();
            ((BasicHttpEntity)mHttpEntity).setContentType(in.readUTF());
            ((BasicHttpEntity)mHttpEntity).setContent(in);
            in.close();
        }
    }

    private static final long serialVersionUID = 1L;
    public static final String GET = "GET";
    public static final String POST = "POST";
    public static final String PUT = "PUT";
    public static final String DELETE = "DELETE";
    private ArrayList<PairValue> queryParams;
    private String requestPath;
    private transient HttpEntity mHttpEntity;
    private String requestMethod;
    private ConcurrentHashMap<String, String> requestHeaders;
}
